<!-- 配置页面路由 -->
<template>
  <div>
    <el-drawer
      :title="form.id ? '编辑' : '新增'"
      v-model="props.visibleDrawer"
      size="50%"
      direction="rtl"
      @close="changeParentProps"
    >
      <template #default>
        <!-- 添加页面 -->
        <el-form :model="form" label-width="80px" colon>
          <el-form-item label="路由">
            <el-input v-model="form.router_path" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="标题">
            <el-input v-model="form.router_title" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="form.router_name" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="模块">
            <el-input v-model="form.router_component" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="图标">
            <el-input v-model="form.router_icon" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="权限">
            <el-checkbox-group v-model="checkbox" size="small">
              <el-checkbox v-for="item in rolesList" :label="item.lable" :key="item.value" border />
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="显示导航">
            <el-switch v-model="form.router_alwaysShow" />
          </el-form-item>
          <el-form-item label="是否隐藏">
            <el-switch v-model="form.router_hidden" />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div style="text-align: left">
          <el-button type="primary" @click="submitEditUser">确定</el-button>
          <el-button @click="changeParentProps">取消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import { defineProps, defineEmits, reactive, ref, watch } from 'vue'
import { editRouter, addRouter } from '@/api/router'
const emitFun = defineEmits(['closeDrawer'])
const props = defineProps({
  // 打开弹窗状态
  visibleDrawer: {
    type: Boolean,
    default: false
  },
  // 编辑的用户
  editPage: {
    type: Object,
    default: () => ({})
  }
})
const form = ref(props.editPage)

const checkbox = ref([])
const rolesList = reactive([
  { lable: '管理员', value: 'admin' },
  { lable: '普通用户', value: 'editor' }
])

// 监听弹窗变化，重新给值
watch(
  () => props.visibleDrawer,
  (val) => {
    if (val && props.editPage.id) {
      let jsonList = []
      try {
        jsonList = JSON.parse(props.editPage.router_roles) || []
      } catch (error) {
        console.log(error, 'error')
      }
      checkbox.value = jsonList || []
      form.value = {
        ...props.editPage,
        router_hidden: props.editPage.router_hidden,
        router_alwaysShow: props.editPage.router_alwaysShow
      }
      console.log(form.value, 'form')
    }
  }
)
// 提交修改
const submitEditUser = async () => {
  console.log(form.value, 'form.value')
  const params: any = {
    router_path: form.value.router_path,
    router_title: form.value.router_title,
    router_name: form.value.router_name,
    router_component: form.value.router_component,
    router_icon: form.value.router_icon,
    router_roles: JSON.stringify(checkbox.value),
    router_alwaysShow: form.value.router_alwaysShow,
    router_hidden: form.value.router_hidden
  }
  let res: any = { errcode: 1, msg: '操作失败' }
  if (props.editPage.id) {
    params.id = form.value.id
    res = await editRouter(params)
  } else {
    res = await addRouter(params)
  }

  if (res.errcode === 0) {
    ElMessage.success('成功')
    emitFun('closeDrawer', true)
  } else {
    ElMessage.error(res.msg)
  }
}
// 取消&关闭
const changeParentProps = () => {
  emitFun('closeDrawer', false)
}
</script>

<style lang="scss" scoped></style>
